//
//  LHGoodsDetailView.m
//  lehuotest
//
//  Created by ice on 2017/1/16.
//  Copyright © 2017年 QF. All rights reserved.
//

#import "LHGoodsDetailView.h"
#import "UIView+Utils.h"
#import "LHImageTextDetailView.h"
#import "LHDetailHeaderView.h"
#import <Masonry.h>
#import "ScrollToTopHelper.h"

#define kEndH 40
#define kImageTextEndH 40
#define kNavBarHeight   0
#define kHeaderViewHeight 200

@interface LHGoodsDetailView()<UIScrollViewDelegate,LHImageTextDetailViewDelegate>
{
    CGFloat minY;
    CGFloat maxY;
    // 是否显示底部视图，
    BOOL _isShowBottom;
}


@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) id topScroller;

@property (nonatomic, strong) id bootomView;


@property (nonatomic, strong) LHDetailHeaderView *headerView;

@property (nonatomic, strong) LHImageTextDetailView *imageTextView;

@property (nonatomic, strong) ScrollToTopHelper *scrollToTopHelp;
@end

@implementation LHGoodsDetailView

- (UIView *)contentView
{
    if (!_contentView)
    {
        _contentView = [[UIView alloc] init];
        _contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 2);
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}

-(LHImageTextDetailView *)imageTextView
{
    if (!_imageTextView){
        _imageTextView = [LHImageTextDetailView loadFromNib];
        _imageTextView.frame = CGRectMake(0, SCREEN_HEIGHT+64, SCREEN_WIDTH, SCREEN_HEIGHT-64);
        _imageTextView.webView.scrollView.delegate = self;
        _imageTextView.delegate = self;
    }
    return _imageTextView;
}

-(LHDetailHeaderView *)headerView
{
    if (!_headerView){
        _headerView = [[LHDetailHeaderView alloc] initWithFrame:CGRectMake(0,kNavBarHeight,SCREEN_WIDTH,kHeaderViewHeight)];
        _headerView.backgroundColor = [UIColor redColor];
    }
    return _headerView;
}

-(instancetype) initWithFrame:(CGRect)frame topView:(id)topView bootomView:(id) bootomView
{
    self = [super initWithFrame:frame];
    if (self){
        _topScroller = topView;
        _bootomView = bootomView;
        [self commitUI];
        //添加入口 -- 使用代码时删掉
//        self.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
//        [self addGestureRecognizer:tap];
    }
    return self;
}

//添加入口 -- 使用代码时删掉
- (void)tapClick
{
    self.tap();
}
-(void) commitUI
{

    [self.topScroller addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];

    ((UIScrollView *)self.topScroller).contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT*2);
    [self.contentView addSubview:self.topScroller];
    
    
    ((UIScrollView *)self.topScroller).contentInset = UIEdgeInsetsMake(kNavBarHeight+kHeaderViewHeight, 0, 0, 0);
    ((UIScrollView *)self.topScroller).contentOffset = CGPointMake(0, -kHeaderViewHeight);

    [self.contentView addSubview:self.imageTextView];
    
    //init headerView
    [self.contentView addSubview:self.headerView];
    [self addSubview:self.contentView];
    

//    self.scrollToTopHelp = [scrollToTopHelper setupOnScrollview:(UIScrollView *)self.topScroller withVisibilityOffset:((UIScrollView *)self.topScroller).height bottomEdge:SCREEN_HEIGHT+40 partyView:self];

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentOffset"])
    {
        CGPoint offset = [change[NSKeyValueChangeNewKey] CGPointValue];
//        NSLog(@"offset.y = %f",offset.y);
        if (offset.y <= 0) {
            if (-offset.y <= kNavBarHeight+kHeaderViewHeight){
                // -264 < y < 0的情况
                CGFloat h = (kNavBarHeight+kHeaderViewHeight+offset.y)/2.0;

                self.headerView.frame = CGRectMake(0, -h, SCREEN_WIDTH, -offset.y-kNavBarHeight+h);

            }else {
                //    y < -264情况
                CGFloat h = (kNavBarHeight+kHeaderViewHeight+offset.y)/2.0;
                self.headerView.frame = CGRectMake(0, -h, SCREEN_WIDTH, kHeaderViewHeight);

            }
        } else {
            //y > 0 的情况
            CGFloat h = (kNavBarHeight+kHeaderViewHeight+offset.y)/2.0;
            self.headerView.frame = CGRectMake(0, -h, SCREEN_WIDTH, -offset.y-kNavBarHeight+h);
        }
//        CGFloat h = (kNavBarHeight+kHeaderViewHeight+offset.y)/2.0;
//        self.headerView.frame = CGRectMake(0, -h, SCREEN_WIDTH, -offset.y-kNavBarHeight+h);
    }
}


#pragma mark - UIScrollerViewDelegate   /*这里针对bottom部分**/
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView == self.imageTextView.webView.scrollView){
        [self LHGoodsDetailViewScrollViewDidScroll:scrollView];
    }
}

-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView == self.imageTextView.webView.scrollView){
        [self LHGoodsDetailViewScrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
}
-(void) LHGoodsDetailViewScrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset = scrollView.contentOffset.y;
    if (offset <= -kImageTextEndH){
        self.imageTextView.showText = @"释放返回商品详情";
    } else {
        self.imageTextView.showText = @"下拉返回商品详情";
    }
    if (offset < 0){
        self.imageTextView.alphaValue = -offset/kImageTextEndH;
    }
}
-(void)LHGoodsDetailViewScrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate){
        CGFloat offset = scrollView.contentOffset.y;
        
        if (scrollView == self.topScroller){
            
            // 滚到底部视图
            if (offset >= ((UIScrollView *)self.topScroller).contentSize.height - SCREEN_HEIGHT + kEndH){
                [self showBootomView];
            }
        }else{
            // 滚到中间视图
            if (offset <= -kImageTextEndH && _isShowBottom){

                [self showTopView];
            }
        }
        
        
    }

}

//显示底部视图
-(void) showBootomView
{
    _isShowBottom = NO;
    [UIView animateWithDuration:0.4 animations:^{
        self.contentView.transform = CGAffineTransformTranslate(self.contentView.transform, 0,-SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        maxY = 0.0f;
        _isShowBottom = YES;
        //                if (!self.hasShowImageText){
        //                    [self.imageTextView loadRequestUrl:@"http://www.baidu.com"];
        //                }
    }];
}
//显示顶部视图
-(void) showTopView
{
    _isShowBottom = NO;
    [UIView animateWithDuration:0.4 animations:^{
        self.contentView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        minY = 0.0f;
    }];

}

#pragma mark - LHImageTextDetailViewDelegate
-(void) LHImageTextDetailView:(LHImageTextDetailView *)imageTextView loadRequest:(NSString *)url isSuccess:(BOOL)isSuccess
{
    if (isSuccess){
        NSLog(@"webView加载完毕");
    }
}


@end
